<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Pubsubhubbub_Publish {
    
    public static function publish($callback, $secret, $raw) {
        
        $signature = hash_hmac('sha1', $raw, $secret);
        
        echo "\n\tReady to call: '" . $callback . "'.";
        $request = curl_init($callback);
        curl_setopt($request, CURLOPT_POST, TRUE);
        curl_setopt($request, CURLOPT_POSTFIELDS, $raw);
        curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
        
        if(!empty($secret)) {
            $signature = hash_hmac('sha1', $raw, $secret);
            $headers[] = 'X-Hub-Signature: sha1=' . $signature;
            curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
        }
        
        echo "\n\tReply from callback: '" . curl_exec($request) ."'.";
        $code = curl_getinfo($request, CURLINFO_HTTP_CODE);
        echo "\n\tReply from code    : " . $code . "'.";
        $ret = true;
        
//        if (in_array($code, array(202, 204))) {
//          echo "Positive response to \"subscribe\" request ($code)";
//          //return true;
//        } else {
//          echo "Error issuing \"subscribe\" request to " . $ps->getHub() . " ($code).";
//          //:print_r($ps->asSubscribePostFields());
//          $ret = false;
//        }   
        curl_close($request);
        return $ret;    
    }
}